// pages/pets-info/pets-info.js
import { Pets } from 'pets-model.js';
import { Diary } from '../diary/diary-model.js';
var diary = new Diary();
var pets = new Pets();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadingHidden: false,
    showModalStatus: false 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.data.id = id;
    this._loadData();
  },

  onShow: function() {
    this._loadData();
  },

  _loadData: function () {
    var that = this;

    pets.getPetsInfo(this.data.id, (res) => {
      console.log(res.skin)
      that.setData({
        loadingHidden: true,
        petsInfo: res.info,
        alllevel: res.alllevel,
        level: res.level,
        growth: res.info.growth,
        collect: res.collect,
        feedTotal: res.feedTotal,
        petsPhoto: res.photo,
        feedData: res.feedData,
        collectNum: res.collectNum,
        isMine: res.self,
        petsSkin: res.info.bg,
        skin: res.skin,
        skinSelect: 0
      })
    })

    diary.getDiaryOneList(this.data.id, (res) => {
      that.setData({
        petsDiary: res.diary,
      })
    })
  },

  photoListTap: function (e) {
    var id = pets.getDataSet(e, 'id');
    wx.navigateTo({
      url: '../photo-list/photo-list?id=' + id,
    })
  },

  petsPhotoTap: function (e) {
    var id = pets.getDataSet(e, 'id');
    wx.navigateTo({
      url: '../photo/photo?id=' + id,
    })
  },
  skinSelectTap: function(e) {
    var that = this;
    that.setData({
      skinSelect: pets.getDataSet(e, 'select'),
      petsSkin: pets.getDataSet(e, 'bg'),
    })
  },

  editPetsTap: function(e) {
    var id = pets.getDataSet(e, 'id');
    wx.navigateTo({
      url: '../edit-pets/edit-pets?id=' + id,
    })
  },

  // 跳转到写日记
  onWriteDiary: function (e) {
    var id = pets.getDataSet(e, 'id');
    wx.navigateTo({
      url: '../create-diary/create-diary?id=' + id,
    })
  },

  // 收藏
  onCollect: function (e) {
    var id = pets.getDataSet(e, 'id');
    pets.collectPet(id, (res) => {
      if (res.code) {
        wx.showToast({
          title: res.info,
          icon: 'success',
          duration: 2000
        });

        this.setData({
          collect: 1
        })
      } else {
        wx.showToast({
          title: res.error,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  diaryTap: function (e) {
    var id = pets.getDataSet(e, 'id');
    wx.navigateTo({
      url: '../diary/diary?id=' + id,
    })
  },

  // 取消收藏
  onCancelCollect: function (e) {
    var id = pets.getDataSet(e, 'id');
    pets.cancelCollect(id, (res) => {
      if (res.code) {
        wx.showToast({
          title: res.info,
          icon: 'success',
          duration: 2000
        });

        this.setData({
          collect: 0
        })
      } else {
        wx.showToast({
          title: res.error,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  // 喂食
  onFeed: function (e) {
    var id = pets.getDataSet(e, 'id');
    pets.feed(id, (res) => {
      if (res.code) {
        wx.showToast({
          title: res.info,
          icon: 'success',
          duration: 2000
        });
        this.setData({
          growth: this.data.growth + 1
        })
      } else {
        wx.showToast({
          title: res.error,
          icon: 'none',
          duration: 2000
        });
      }
    })
  },

  powerDrawer: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu)
  },
  chooseDrawer: function(e) {
    var that = this;
    var currentStatu = e.currentTarget.dataset.statu;
    var pets_id = that.data.petsInfo.id;
    this.util(currentStatu);
    if (!this.data.skinSelect) {
      
      return false;
    }
    pets.setSkin(pets_id, this.data.skinSelect, (res) => {
      if(res.status) {
        that.setData({
          petsSkin: res.bg
        })
      }else{
        wx.showToast({
          title: res.error,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({
      duration: 0,  //动画时长  
      timingFunction: "linear", //线性  
      delay: 0  //0则不延迟  
    });

    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;

    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })

      //关闭  
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 0)

    // 显示  
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  }  
})